<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {

	function __construct(){
		parent::__construct();

		/** 登陆验证 */
		if(!isset($_SESSION)){
		    session_start();
		}
		$admin = $this->session->userdata('admin_user');
		if(!$admin || !isset($admin['user_id'])){
			header('Location: /ychadmin/login');
			exit;
		}
		$this->tab = 'user';


	}


	/**
	 * 用户列表
	 */
	function ulist(){
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;

		$parms     = array(
			'username'   => trim($this->input->get('username')),
			'realname'   => trim($this->input->get('realname')),
			'email'      => trim($this->input->get('email')),
			'type_id'	 => intval($this->input->get('type_id')),
			'add_time'	 => $this->input->get('add_time'),

		);

		$data = array();

		$this->load->model('User_model', 'user');
		$this->load->model('Area_model', 'area');
		$data = $this->user->get_user_list($parms,$per_page);

		$data['area_list'] = $this->area->get_list();
		$data['parms'] = $parms;

		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/user/ulist?username='.$parms['username'].'&realname='.$parms['realname'].'&email='.$parms['email'].'&type_id='.$parms['type_id'].'&add_time='.$parms['add_time'];
		$config['total_rows'] = $data['user_count'];
		$config['per_page'] = $page_size;
		$this->pagination->initialize($config);
		$data['title'] = '用户列表';

		$data['tab'] = $this->tab;
		$data['sub'] = 'ulist';

		$this->load->view('/ychadmin/user_list', $data);
	}

	/**
	 * 用户认证信息列表
	 */
	function attestation_all(){
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		$parms     = array(
			'username'   => trim($this->input->get('username')),
			'realname'   => trim($this->input->get('realname')),
		);
		$data = array();
		$this->load->model('User_model', 'user');
		$data = $this->user->user_attestation_status($parms,$per_page);
		$data['parms'] = $parms;
		$data['kefu_list'] = $this->user->get_kefu_list();
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/user/attestation_all?username='.$parms['username'].'&realname='.$parms['realname'];
		$config['total_rows'] = $data['count'];
		$config['per_page'] = $page_size;
		$this->pagination->initialize($config);
		$data['title'] = '用户认证信息列表';
		$data['p'] = $this->user->arrays();
		//var_dump($data['v']);exit;

		$data['tab'] = $this->tab;
		$data['sub'] = 'attestation';

		$this->load->view('/ychadmin/attestation_all', $data);
	}

	/**
	 * 奖励VIP期限
	 */
	function award_vip(){
		$day = $this->input->post('day');
		$user_id = $this->input->post('uid');
		//echo $day;
		$viptime = 3600*24*$day;//VIP奖励时间转成秒
		$this->load->model('User_vip_model');
		$vipinfo = $this->User_vip_model->get_by_uid($user_id);
		$time = time();
		//var_dump($vipInfo);exit;
		//echo $vipinfo['vip_status']."====".$time-$vipinfo['vip_verify_time'];exit;
		if ($vipinfo && $vipinfo['vip_status']==1 && ($time-$vipinfo['vip_verify_time']<=3600*24*365)){
			//echo 1;exit;
			//本身是有效VIP，直接延期
			$vipSetArr = array(
				'vip_verify_time' => $vipinfo['vip_verify_time']+ $viptime
			);
			$this->User_vip_model->update($vipSetArr,array('user_id'=>$user_id));
		}elseif ($vipinfo && $vipinfo['vip_status']==1 && ($time-$vipinfo['vip_verify_time']>3600*24*365)){
			//echo 2;exit;
			//已过期的  设置成奖励时间之后过期
			$vipSetArr = array(
				'vip_verify_time' => time()-(3600*24*365) + $viptime
			);
			$this->User_vip_model->update($vipSetArr,array('user_id'=>$user_id));
		}elseif (!$vipinfo){
			//echo 3;exit;
			//不是VIP的 自动成为VIP 但是奖励时间过后过期
			//$this->load->model('User_model');
			//$kefuList = $this->User_model->get_kefu_list();
			$kefu_userid = 0;//array_rand($kefuList);//随机
			$verifyTime = time()-(3600*24*365)+$viptime;
			$vipSetArr = array(
				'user_id' => $user_id,
				'kefu_userid' => $kefu_userid,
				'kefu_addtime' => $verifyTime,
				'vip_status' => 1,//自动通过无需人工审核
				'vip_remark' => '',
				'vip_money' => 0,
				'vip_verify_remark' => 'VIP审核通过',
				'vip_verify_time' => $verifyTime,
			);
			$this->User_vip_model->apply($vipSetArr);
		}
		echo "奖励成功";
	}

	/**
	 * 认证详情
	 */
	function attestation_info(){
		$user_id = intval($this->input->get('user_id'));
		if($user_id<1){
			$this->js->alert('数据错误');
			$this->js->go('/ychadmin/user/attestation_all');
		}
		$this->load->model('User_model', 'user');
		$data['info'] = $this->user->user_attestation_info($user_id);
		$this->load->model('Area_model', 'area');
		$data['area_list'] = $this->area->get_list();
		$this->load->model('Linkage_model', 'linkage');
		$data['minzu'] = $this->linkage->get_linkage_list(31);
		$data['hunyan'] = $this->linkage->get_linkage_list(1);
		$data['zinv'] = $this->linkage->get_linkage_list(2);
		$data['xueli'] = $this->linkage->get_linkage_list(3);
		$data['zhufang'] = $this->linkage->get_linkage_list(6);
		$data['car'] = $this->linkage->get_linkage_list(7);
		$data['gsxz'] = $this->linkage->get_linkage_list(10);
		$data['gshy'] = $this->linkage->get_linkage_list(11);
		$data['zhiwei'] = $this->linkage->get_linkage_list(13);
		$data['guanxi'] = $this->linkage->get_linkage_list(18);
                $data['shouru'] = $this->linkage->get_linkage_list(4);
		$data['title'] = '用户认证-详细信息';
		$data['user_array'] = $this->user->arrays();
		$data['tab'] = $this->tab;
		$data['sub'] = 'attestation';

		$this->load->view('/ychadmin/attestation_info', $data);
	}

	/**
	 * 实名认证列表
	 */
	function real_status(){
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		$parms     = array(
			'username'     => trim($this->input->get('username')),
			'real_status'  => intval($this->input->get('real_status')),
		);
		$data = array();
		$this->load->model('User_model', 'user');
		$data = $this->user->real_status_list($parms,$per_page);
		$this->load->model('Area_model', 'area');
		$data['area_list'] = $this->area->get_list();
		$this->load->model('Linkage_model', 'linkage');
		$data['minzu'] = $this->linkage->get_linkage_list(31);
		$data['parms'] = $parms;

		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/user/real_status?real_status='.$parms['real_status'].'&username='.$parms['username'];
		$config['total_rows'] = $data['count'];
		$config['per_page'] = $page_size;
		$this->pagination->initialize($config);
		$data['title'] = '用户认证-实名认证';
		$data['user_array'] = $this->user->arrays();

		$data['tab'] = $this->tab;
		$data['sub'] = 'real';

		$this->load->view('/ychadmin/attestation_realname', $data);
	}

	/**
	 * 实名认证
	 */
	function attestation_realname(){
		$parms     = array(
			'user_id'      => intval($this->input->post('user_id')),
			'real_status'  => intval($this->input->post('real_status')),
			'jifen'        => intval($this->input->post('jifen')),
			'remark'       => trim($this->input->post('remark')),
		);

		if($parms['user_id'] < 1 || !in_array($parms['real_status'],array(0,1,2))){
			echo "数据错误";
			exit;
		}
		//获取操作人
		$adminUser = $this->session->userdata('admin_user');
		if(!isset($adminUser['user_id']) || $adminUser['user_id'] < 1){
			echo "错误";
			exit;
		}
		$this->load->model('User_model', 'user');
		$result = $this->user->update(array('real_status'=>$parms['real_status']),array('user_id'=>$parms['user_id']));
		if($result && $parms['real_status']==1){
			$this->Credit($parms['user_id'], 'real');
		}
		//发送站内信提醒
		$this->letterStation($parms['user_id'], '实名认证', $parms['remark']);

		echo $result ? '操作成功' : '操作失败';
	}

	function edit($user_id=''){
		$data = array();
		$user_id = intval($user_id);
		$this->load->model('User_model', 'user');
        $this->load->model('Linkage_model', 'linkage');
		$info = $this->user->get_by_uid($user_id);
		$act = $this->input->post('act');
		if($act == 'edit'){
			$setArr = array(
				'email' => $this->input->post('email'),
				'realname' => $this->input->post('realname'),
				'sex' => $this->input->post('sex'),
				'birthday' => $this->input->post('birthday') ? strtotime($this->input->post('birthday')) : 0,
				'is_loan' => $this->input->post('is_loan'),
				'fstatus' => $this->input->post('fstatus'),
				'is_self' => $this->input->post('is_self'),
				'card_type' => $this->input->post('card_type'),
				'card_id' => $this->input->post('card_id'),
				'phone' => $this->input->post('phone'),
				'phone_status' => $this->input->post('phone_status'),
				'real_status' => $this->input->post('real_status'),
                'email_status' => $this->input->post('email_status'),
				'qq' => $this->input->post('qq'),
				'error_num'=>$this->input->post('error_num'),
				'type_id'=>$this->input->post('type_id'),
                'nation' => $this->input->post('nation'),
                'province' => $this->input->post('province'),
                'city' => $this->input->post('city'),
                'area' => $this->input->post('area'),
			);

			if ($setArr['fstatus']==3){
				$adminUser = $this->session->userdata('admin_user');
				if($adminUser['type_id']!=1){
					$ret = array('msg'=>'权限不足','url'=>'');
					echo json_encode($ret);
					exit;
				}
			}
			if(!$setArr['email']){
				$ret = array('msg'=>'邮箱不能为空','url'=>'');
				echo json_encode($ret);
				exit;
			}
			$pwd = $this->input->post('password');
			$pwd2 = $this->input->post('password2');
			if($pwd && $pwd==$pwd2){
				$setArr['password'] = md5(md5($pwd));
			}else{
				if($pwd && $pwd!=$pwd2){
					$ret = array('msg'=>'两次密码输入不一致','url'=>'');
					echo json_encode($ret);
					exit;
				}
			}

			$this->user->update($setArr,array('user_id'=>$user_id));
			$ret = array('msg'=>'修改成功','url'=>'');
			$this->load->model('log_model');$this->log_model->add_log('[修改][用户]ID=='.$user_id);
			echo json_encode($ret);
			exit;
		}
        $data['minzu'] = $this->linkage->get_linkage_list(31);//民族选项

		$data['v'] = $this->user->arrays();
		$data['info'] = $info;
		$data['title'] = '用户信息-基础信息';

		$data['tab'] = $this->tab;
		$data['sub'] = 'ulist';

		$this->load->view('/ychadmin/user_edit', $data);
	}

	function add(){
		$data = array();
		$this->load->model('User_model', 'user');
		$act = $this->input->post('act');
		if($act == 'add'){
			$setArr = array(
				'username' => $this->input->post('username'),
				'email' => $this->input->post('email'),
				'realname' => $this->input->post('realname'),
				'sex' => $this->input->post('sex'),
				'birthday' => $this->input->post('birthday') ? strtotime($this->input->post('birthday')) : 0,
				'type_id' => $this->input->post('type_id'),
				'fstatus' => $this->input->post('fstatus'),
				'card_type' => $this->input->post('card_type'),
				'card_id' => $this->input->post('card_id'),
				'phone' => $this->input->post('phone'),
				'qq' => $this->input->post('qq')
			);

			$adminUser = $this->session->userdata('admin_user');
			if ($setArr['type_id']==1 && $adminUser['user_id']!=1){
				$ret = array('msg'=>'权限不足','url'=>'');
				echo json_encode($ret);
				exit;
			}
			if ($setArr['type_id']!=3 && $adminUser['type_id']!=1){
				$ret = array('msg'=>'权限不足','url'=>'');
				echo json_encode($ret);
				exit;
			}


			if ($setArr['username']==""){
				$ret['msg'] = '用户名不能为空';
				echo json_encode($ret);exit;
			}

			if(!$setArr['email']){
				$ret = array('msg'=>'邮箱不能为空','url'=>'');
				echo json_encode($ret);
				exit;
			}

            if(!$setArr['phone']){
                $ret = array('msg'=>'手机不能为空','url'=>'');
                echo json_encode($ret);
                exit;
            }

			if($this->user->get_one(array('username'=>$setArr['username']), 'user_id')){
				$ret['msg'] = '用户名已存在';
				echo json_encode($ret);exit;
			}elseif($this->user->get_one(array('email'=>$setArr['email']), 'user_id')){
				$ret['msg'] = '邮箱已存在';
				echo json_encode($ret);exit;
			}elseif($this->user->get_one(array('phone'=>$setArr['phone']), 'user_id')){
                $ret['msg'] = '手机已存在';
                echo json_encode($ret);exit;
            }

			$pwd = $this->input->post('password');
			$pwd2 = $this->input->post('password2');
			if($pwd && $pwd==$pwd2){
				$setArr['password'] = md5(md5($pwd));
			}else{
				if($pwd && $pwd!=$pwd2){
					$ret = array('msg'=>'两次密码输入不一致','url'=>'');
					echo json_encode($ret);
					exit;
				}
			}
			$setArr['add_time'] =time();
			$this->user->add($setArr);
			$ret = array('msg'=>'添加成功','url'=>'');
			$username =$this->input->post('username');
			$this->load->model('log_model');$this->log_model->add_log('[添加][用户]['.$username.']');
			echo json_encode($ret);
			exit;
		}
		$data['v'] = $this->user->arrays();
		$data['title'] = '用户信息-基础信息';

		$data['tab'] = $this->tab;
		$data['sub'] = 'user_add';

		$this->load->view('/ychadmin/user_add', $data);
	}

	function attlist($user_id=''){
		$data = array();
		$user_id = intval($user_id);
		$this->load->model('User_model', 'user');
		$data['userinfo'] = $this->user->get_by_uid($user_id);
		$this->load->model('Attestation_model', 'attestation');
		$this->load->model('Attestation_type_model', 'attestation_type');
		$data['type_list'] = $this->attestation_type->get_id_names();
		$data['status_list'] =  $this->attestation->fstatus();
		$list = $this->attestation->get_list(array('user_id'=>$user_id));
		$data['list'] = $list ? $list : array();

		$data['tab'] = $this->tab;
		$data['sub'] = 'ulist';

		$this->load->view('/ychadmin/user_attlist',$data);
	}

	function update_att($id){
		$id = intval($id);
		$this->load->model('Attestation_model', 'attestation');
		$this->load->model('Attestation_type_model', 'attestation_type');
		$this->load->model('User_model', 'user');
		$att_info = $this->attestation->get_one(array('id'=>$id));

		$user = $this->user->get_by_uid($att_info['user_id']);
		$att_info['username'] = $user['username'];

		if(isset($_POST['act']) && $_POST['act'] == 'edit'){
			if($att_info){
				$parms = array( 'type_id' => $this->input->post('type_id'),
								'name'    => $this->input->post('name'));

				$user_id = $this->input->post('user_id');
				$edit_id = $this->input->post('id');

				if(isset($_FILES['litpic']) && $_FILES['litpic']['name'] != ''){
					$ret_litpic = $this->upload_litpic_data($user_id);
					if($ret_litpic['msg'] == 'ok'){
						$parms['litpic'] = $ret_litpic['litpic'];
					}else{
						$this->js->alert($ret_litpic['msg']);exit;
					}
				}

				$where = array('id'=>$edit_id,'user_id'=>$user_id);
				$res = $this->attestation->update($parms,$where);
				if($res){
					$ret['msg'] = '保存成功';
					$ret['url'] = '/ychadmin/user/update_att/'.$edit_id;
					$this->js->alert($ret['msg']);
					$this->js->go($ret['url']);
					exit;

				}else{
					$ret['msg'] = '保存失败';
					$this->js->alert($ret['msg']);exit;
				}


			}
		}else{
			$data['type_list'] = $this->attestation_type->get_id_names();
			$data['att_info'] =$att_info;
			$data['tab'] = $this->tab;
			$data['sub'] = 'documentation';
			$this->load->view('/ychadmin/user_attedit',$data);
		}

	}


	function del_att($id='', $userId = ''){
		$id = intval($id);
		$this->load->model('Attestation_model', 'attestation');
		$res = $this->attestation->delete(array('id'=>$id, 'user_id' => $userId));
		$act = $this->input->post('act');
		$username = $this->input->post('username');
		if($res){
			$ret = array('msg'=>'删除成功','url'=>'');
			$this->load->model('log_model');$this->log_model->add_log('用户'.$username.'删除成功');
			echo json_encode($ret);
			exit;
		}else{
			$ret = array('msg'=>'删除失败','url'=>'');
			$this->load->model('log_model');$this->log_model->add_log('用户'.$username.'删除失败');
			echo json_encode($ret);
			exit;
		}

	}

	function check_att(){
		$this->load->model('Attestation_model', 'attestation');
		$this->load->model('Credit_model', 'credit');

		$id = $this->input->post("id");
		$fstatus = $this->input->post('fstatus');

		$setArr = array(
				'fstatus'  => $fstatus,
				'verify_time' => time(),
				'verify_user' => 1,
				'verify_remark' => $this->input->post('verify_remark'),
			);


		if($id>0){
			$attinfo = $this->attestation->get_one(array('id'=>$id));
			$where = array('user_id'=>$attinfo['user_id'],'id'=>$id);
			$res = $this->attestation->update($setArr,$where);
			if($res>0){
				if($fstatus == 1){
					$jifen = $attinfo['jifen'];
					//积分增加
					if($jifen>0){
						$this->load->model('Attestation_type_model', 'attestation_type');

						$type_list = $this->attestation_type->get_id_names();

						$this->notice_doc_credit($attinfo['user_id'], $jifen,$type_list[$attinfo['type_id']]);
					}
				}

				echo  '操作成功';
				exit;
			}else{
				echo  '操作失败';
				exit;
			}
		}

	}

	function showDetailInfo(){
		$user_id = intval($this->input->post('user_id'));

		$this->load->model('User_model');
		$this->load->model('Borrow_model');
		$this->load->model('User_vip_model');
		$this->load->model('Credit_model');
		$this->load->model('Area_model', 'area');
		$this->load->model('Account_model');
		$this->load->model('Accountcash_model');

		$data['info'] = $this->User_model->user_attestation_info($user_id);
		$data['sex'] = $this->User_model->arrays('sex');
		$data['statusArr'] = $this->User_model->arrays('statusArr');
		$data['cardType'] = $this->User_model->arrays('cardType');

		$data['u_credit'] = $this->Credit_model->get_row($user_id);
		$data['u_credit']['credit_value'] = isset($data['u_credit']['credit_value']) ? $data['u_credit']['credit_value'] : 0;
		$data['u_credit']['pic'] = $this->Credit_model->get_pic($data['u_credit']['credit_value']);

		$data['tender_list'] = $this->Borrow_model->get_borrow_tender_list(array('select'=>1,'page_size'=>6,'page_start'=>0,'where'=>array('user_id'=>$user_id,'fstatus'=>1)));

		$cdata['where']['username']=$data['info']['username'];
		$cdata['page_start'] = 0;
		$cdata['page_size'] = 1;
		$account = $this->Account_model->get_account_user_list($cdata);
		$data['account_list'] = $account['account_list'];
		$data['area_list'] = $this->area->get_list();

		$data['name_list'] = $this->User_model->get_name_list();

		$data['account_bank'] = $this->Accountcash_model->arrays("account_bank");

		$response_html = $this->load->view('/ychadmin/show_user_info',$data,true);

		echo $response_html;
	}


	function getinfo(){
		$user_id = intval($this->input->post('user_id'));
		$ret['msg'] = '数据错误';
		if($user_id>0){
			$this->load->model('User_model');
			$user = $this->User_model->get_by_uid($user_id);
			if($user){
				$tmp = array(0=>'-',1=>'男',2=>'女');
				$user['sex'] = $tmp[$user['sex']];

				if($user['nation']){
					$this->load->model('Linkage_model', 'linkage');
					$tmp = $this->linkage->get_linkage_list(31);
					$user['nation'] = $tmp[$user['nation']];
				}else{
					$user['nation'] = '-';
				}

				$user['birthday'] = $user['birthday'] ? date('Y-m-d',$user['birthday']) : '-';

				$jiguan = '';
				$this->load->model('Area_model', 'area');
				$areaList = $this->area->get_list();
				$user['province'] && $jiguan .= $areaList[$user['province']];
				$user['city'] && $jiguan .= $areaList[$user['city']];
				$user['area'] && $jiguan .= $areaList[$user['area']];
				$user['jiguan'] = $jiguan ? $jiguan : '-';


				$ret['msg'] = '';
				$ret['user'] = $user;

				$this->load->model('User_vip_model');
				$uservip = $this->User_vip_model->get_by_uid($user_id);
				if($uservip){
					$kefuList = $this->User_model->get_kefu_list();
					$user['kefu'] = $kefuList[$uservip['kefu_userid']]['username'];
				}
				$ret['user'] = $user;
			}

		}
		echo json_encode($ret);
	}

	function leve(){

		$this->load->model('Credit_model');

		$data['tab'] = $this->tab;
		$data['sub'] = 'user_rank';

		$data['list'] = $this->Credit_model->get_credit_rank();
		$this->load->view('/ychadmin/credit_rank',$data);
	}

	function leve_synchronize(){
		$this->load->model('Credit_model');
		$id = $this->input->post("id");
		$info = $this->Credit_model->get_credit_rank_one($id);
		$ret = array('msg'=>$info);
		echo json_encode($ret);
		exit;
	}

	function credit_rank_save(){
		$this->load->model('Credit_model');
		$admin = $this->session->userdata('admin_user');
		if(isset($admin['type_id']) && $admin['type_id'] ==1){
			$act = $this->input->post("act");
			$parms = array(	'creidt_rank'=>$this->input->post("creidt_rank"),
							'point1'=>$this->input->post("point1"),
							'point2'=>$this->input->post("point2"),
							'pic'=>$this->input->post("pic"),
						);

			if($act == 'edit'){
				$id = $this->input->post("id");
				$res = $this->Credit_model->update_credit_rank($id,$parms);
			}else if($act == 'delete'){
				$id = $this->input->post("id");
				$res = $this->Credit_model->del_credit_rank($id);
			}else{
				$res = $this->Credit_model->add_credit_rank($parms);
			}
			if($res>0){
				$ret = array('msg'=>'操作成功');
			}else{
				$ret = array('msg'=>'操作失败');
			}
			echo json_encode($ret);
			exit;
		}else{
			$ret = array('msg'=>'对不起，权限不足');
			echo json_encode($ret);
			exit;
		}


	}

	function documentation(){
		$this->load->model('Attestation_model', 'attestation');
		$this->load->model('Attestation_type_model', 'attestation_type');

		$this->load->model('User_model', 'user');

		$username = $this->input->get("username");


		$parms = array('is_del'=>0);

		if($username){
			$parms['username']=$username;
		}

		$fstatus = $this->input->get("fstatus");
		if($fstatus !=-1){
			$parms['fstatus'] = $fstatus;
		}


		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;

		$parms['guarantee_id'] = 0;
		$vdata = $this->attestation->get_attestation_list($parms,$per_page,$page_size);

		$vdata['kefu_list'] = $this->user->get_kefu_list();
		$vdata['type_list'] = $this->attestation_type->get_id_names();
		$vdata['status_list'] =  $this->attestation->fstatus();
		//分页
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/user/documentation?username='.$parms['username'].'&fstatus='. $fstatus;
		$config['total_rows'] = $vdata['count'];
		$config['per_page'] = $page_size;
		$this->pagination->initialize($config);

		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'documentation';
		$parms['fstatus'] = $fstatus;
		$vdata['parms'] = $parms;
		$this->load->view('/ychadmin/user_document', $vdata);

	}

	function data_upload(){
		$this->load->model('Attestation_model', 'attestation');
		$this->load->model('Attestation_type_model', 'attestation_type');

		$vdata['type_list'] = $this->attestation_type->get_id_names();
		$vdata['tab'] = $this->tab;
		$vdata['sub'] = 'documentation';
		$this->load->view('/ychadmin/user_dataupload', $vdata);
	}

	function doDataUpload(){
		$ret = array('msg' => '','url' => '');

			$username = $this->input->post("username");
			if(!$username){
				$ret['msg'] = '用户名不能为空';
				$this->js->alert($ret['msg']);exit;
			}
			$this->load->model('Credit_model', 'credit');
			$credit_info = $this->credit->get_credit_rule(array('nid'=>'zhengjian'));
			$type_id=$credit_info['id'];

			/*$jifen = $credit_info['credit_value'];*/
			$jifen = 0;

			$this->load->model('User_model', 'user');
			$userinfo = $this->user->get_one(array('username'=>$username));
			if(!$userinfo){
				$ret['msg'] = '此用户名不存在';
				$this->js->alert($ret['msg']);exit;
			}


			$setArr = array(
				'user_id' => $userinfo['user_id'],
				'type_id' => $this->input->post('type_id'),
				'name'    => $this->input->post('name'),
				'fstatus'  => 0,
				'jifen'   => $jifen,
				'content' => $this->input->post('name'),
				'verify_time' => 0,
				'verify_user' => 1,
				'verify_remark' => '待审核',
				'add_time' => time(),
				'addip' => $this->input->ip_address(),
			);

			$this->load->model('Attestation_model', 'attestation');
			$ret_litpic = $this->upload_litpic_data($userinfo['user_id']);
			if($ret_litpic['msg'] == 'ok'){
				$setArr['litpic'] = $ret_litpic['litpic'];
				$res = $this->attestation->add($setArr);
				if($res){
					$ret['msg'] = '上传成功';
					$this->js->alert($ret['msg']);exit;

				}else{
					$ret['msg'] = '保存失败';
					$this->js->alert($ret['msg']);exit;
				}
			}else{
				$this->js->alert($ret_litpic['msg']);exit;
			}
	}

	private function upload_litpic_data($user_id){
			$path = 'static/data/upfiles/images/'.$user_id.'/';
			if(!file_exists($path)){
				mkdir($path,0777);
			}
			$config['upload_path'] = $path;
			$config['allowed_types'] = 'jpg|png';
			$config['file_name'] = 'attestation_'.uniqid();
			$config['overwrite'] = true;
			$config['max_size'] = '512';
			$config['max_width']  = '2048';
			$config['max_height']  = '3000';

			$this->load->library('upload', $config);
			if($this->upload->do_upload('litpic')){
				$tmp = $this->upload->data();
				$litpic = 'static/data/upfiles/images/'.$user_id.'/'.$config['file_name'].$tmp['file_ext'];

				//添加水印
				$this->load->library('image_lib');
				$warter['source_image'] = $litpic;
				$warter['wm_type'] = 'overlay';
				$warter['wm_overlay_path'] = WATERMARK_IMG;
				$warter['wm_opacity'] = '60';
				$warter['wm_vrt_alignment'] = 'middle';
				$warter['wm_hor_alignment'] = 'center';
				$this->image_lib->initialize($warter);
				$this->image_lib->watermark();
				$msg = 'ok';
			}else{
				$litpic = '';
				$msg = $this->upload->display_errors();
			}

			return array('litpic'=>$litpic,'msg'=>$msg);
	}

	//vip列表
	function vip_ulist(){
		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;
		$parms     = array(
			'username'     => trim($this->input->get('username')),
			'vip_status'  => intval($this->input->get('vip_status')),
		);
		$data = array();
		$this->load->model('User_model', 'user');
		$data = $this->user->vip_status_list($parms,$per_page);
		$data['parms'] = $parms;
		$data['kefu_list'] = $this->user_model->get_kefu_list();
		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/user/vip_ulist?username='.$parms['username'].'&vip_status='.$parms['vip_status'];
		$config['total_rows'] = $data['count'];
		$config['per_page'] = $page_size;
		$this->pagination->initialize($config);
		$data['title'] = '用户管理-VIP审核';
		$data['statusArr'] = $this->user->arrays('statusArr');
		$data['tab'] = $this->tab;
		$data['sub'] = 'user_vip';
		$this->load->view('/ychadmin/attestation_vip', $data);
	}

	function vip_status(){
		$userid 			= $this->input->post("user_id");
		$vip_status 		= $this->input->post("vip_status");
		$vip_verify_remark	= $this->input->post("vip_verify_remark");

		$this->load->model('User_vip_model', 'vip');
		$setArr = array(
				'vip_status' => $vip_status,
				'vip_verify_remark' => $vip_verify_remark,
				'vip_verify_time' => time(),
			);
		$where = array('user_id'=>$userid);
		$res = $this->vip->update($setArr,$where);
		if($res){
			$name = $vip_status?'VIP审核通过':'VIP审核未通过';
			$content = $vip_status?'您的vip申请已审核通过，祝你投资愉快！':'您的vip申请未审核通过!';
			$this->letterStation($userid, $name, $content);

			if($vip_status){$this->Notice(8, $userid);}
			//VIP积分日志
			if($vip_status){$this->Credit($user_id,'vip');}
			echo '操作成功';
			exit;
		}else{
			echo '操作失败';
			exit;
		}

	}

	public function datum(){
		$this->load->model('attestation_type_model');

		$per_page  = intval($this->input->get('per_page'));
		$page_size = 10;

		$data = $this->attestation_type_model->get_datum_list(array('fstatus'=>1),$per_page,$page_size);

		$this->load->library('pagination');
		$config['base_url'] = '/ychadmin/user/datum?';
		$config['total_rows'] = $data['count'];
		$config['per_page'] = $page_size;
		$this->pagination->initialize($config);

		$data['tab'] = $this->tab;
		$data['sub'] = 'user_datum';

		$this->load->view('/ychadmin/user_datum',$data);
	}

	public function user_datum_save(){
		$this->load->model('attestation_type_model');

		$parms = array(	'name'=>$this->input->post("name"),'jifen'=>$this->input->post("jifen"));
		$id = intval($_POST['id']);
		$act = $_POST['act'];
		if(isset($act) && $act == 'edit'){
			$res = $this->attestation_type_model->update($parms,array('type_id'=>$id));
		}elseif($act == 'del'){
			$res = $this->attestation_type_model->update(array('fstatus'=>2),array('type_id'=>$id));
		}elseif($act == 'add'){
			$parms['fstatus'] = 1;
			$res = $this->attestation_type_model->add($parms);
		}

		$this->mycache->delete('ych_attestation_type');

		if($res>0){
			$ret = array('msg'=>'操作成功');
		}else{
			$ret = array('msg'=>'操作失败');
		}
		echo json_encode($ret);
		exit;
	}
}
